package com.kingcore.director.main;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Observable;
import java.util.Observer;
import java.util.ResourceBundle;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JRootPane;
import javax.swing.JTextField;
import javax.swing.undo.UndoManager;

import wzw.util.I18nUtil;
import wzw.util.I18nUtils;

import com.kingcore.director.DirectorConstants;
import com.kingcore.director.base.ui.DirectorDialog;
import com.kingcore.director.core.util.DirectorComponentUtils;

/**
 * <p>需要的时候可以考虑使用迷你窗口增加用户体用。
 *     Dialog跟Frame不同在于不会出现在任务栏上。</p>
 * @author Zeven
 *
 */
public class MiniDialog extends DirectorDialog implements ActionListener,KeyListener, 
			Observer, MouseListener, MouseMotionListener 
 { 
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static MiniDialog miniFrame = null;
	
	/**
	 * 采用单件模式。
	 * @return
	 */
	public static MiniDialog getInstance(){
		if (miniFrame==null) {
			miniFrame = new MiniDialog();
		}
		return miniFrame;
	}
	
	private MiniDialog(){
		initializeInterface();
		this.update(null,null);  //构建时候初始一次语言等
		DirectorComponentUtils.move2Centre(this);
	}

	JLabel lb_name = null;
	JLabel lb_ico = null;
	static Point origin = new Point();  //全局的位置变量，用于表示鼠标在窗口上的位置
	private void initializeInterface() {
		this.setLayout(null);
		this.setIconImage(Toolkit.getDefaultToolkit().createImage(MainFrame.appIco));
		this.setSize(272, 48);
		lb_ico = new JLabel(new ImageIcon(MainFrame.appTrayIco)); 
		lb_ico.setBounds(new java.awt.Rectangle(2, 1, 17, 20));
		lb_ico.addMouseListener(this);
		lb_ico.addMouseMotionListener(this);
		lb_name = new JLabel("Mini Window for Director"); 
		lb_name.setBounds(new java.awt.Rectangle(22, 3, 220, 20));
		this.getContentPane().add(lb_ico);
		this.getContentPane().add(lb_name); 
		this.getContentPane().add(this.getCloseButton());
		
		lb_name.addMouseListener(this);
		lb_name.addMouseMotionListener(this);

		this.getContentPane().add( getInputTextField());
		this.getContentPane().add( getSearchButton() );
		
		this.setUndecorated(true); // 去掉窗口的装饰
		this.getRootPane().setWindowDecorationStyle(JRootPane.NONE); // 采用指定的窗口装饰风格
 
		this.getContentPane().setBackground(new Color(210,233,255)); //.LIGHT_GRAY
		
		this.setAlwaysOnTop(true);
	}
	
	private JButton searchButton = null;
	private JButton getSearchButton() {
		if (searchButton==null) {
			searchButton = new JButton();
			searchButton.setText("Search");
			searchButton.setBounds(new java.awt.Rectangle(195, 23, 75, 23));
			searchButton.addActionListener(this);
			searchButton.addKeyListener(this);
		}
		return searchButton;
	}

	private JTextField inputText = null;
	public JTextField getInputTextField() {
		if (inputText==null) {
			inputText= new JTextField(""); //abc
			inputText.setBounds(new java.awt.Rectangle(2, 22, 195, 25));
			inputText.addActionListener(this);
			inputText.addKeyListener(this);
			inputText.getDocument().addUndoableEditListener( getUndoManager()  );
			
			inputText.requestFocusInWindow();
			inputText.selectAll();
		}
		return inputText;
	}

	private JButton btnClose;
	private JButton getCloseButton() {
		if (btnClose==null) {
			btnClose = new JButton();
			btnClose.setBounds(new java.awt.Rectangle(253, 5, 15,15));
			btnClose.setIcon( new ImageIcon("./resource/closeSearch.gif"));
			btnClose.addActionListener(this); ;
		}
		return btnClose;
	}

	public static void main(String[] args) {
		MiniDialog miniFrame = MiniDialog.getInstance();
		miniFrame.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object eventObj = e.getSource();
		if (btnClose==eventObj) {
			this.setVisible(false);
			
		}else if (eventObj==this.inputText || eventObj==this.searchButton) {
			String str = this.inputText.getText();
			if (str==null || str.trim().length()<1) {
				return;
			}
			DirectorComponentUtils.getDefaultWebSearchProvider().searchWebPage(str);
			this.setVisible(false);
			return;
		}
		
	}

	@Override
    public void setVisible(boolean b) {
    	DirectorComponentUtils.move2Centre(this);
		inputText.requestFocusInWindow();
		inputText.selectAll();
    	super.setVisible(b);
    }

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
		Object eventObj = e.getSource();
		int keyCode = e.getKeyCode();

		if ( keyCode== KeyEvent.VK_ESCAPE) {
			if (eventObj==this.inputText || eventObj==this.searchButton) {
				this.setVisible(false);
				e.consume();
				return;
			}
		}else if(keyCode==KeyEvent.VK_Z && DirectorComponentUtils.isControlDownOnly(e)){ //all objects
			editUndo();
			e.consume(); 
		}else if(keyCode==KeyEvent.VK_Y && DirectorComponentUtils.isControlDownOnly(e)){ //all objects
			editRedo();
			e.consume();  
		}
	}

	// ------------------- implement for redo and undo begin
	protected void editUndo(){
	   if (undoManager.canUndo()){
		   undoManager.undo();
		}
	}
	
	protected void editRedo(){
	   if (undoManager.canRedo()){
		   undoManager.redo();
		}
	}
	private UndoManager undoManager = null; 
	private UndoManager getUndoManager(){
		if (undoManager==null) {
			undoManager = new UndoManager(); // {
//				  private static final long serialVersionUID = -5960092671497318496L;
//				  public void undoableEditHappened(UndoableEditEvent e) {
//					  this.addEdit(e.getEdit());
//				  }
//				 };
		}
		return undoManager;
	}
	// ------------------- implement for redo and undo end
	
	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void update(Observable o, Object arg) {
		ResourceBundle bundle = I18nUtils.getBundle(DirectorConstants.I18n_Language_Name);

		this.lb_name.setText(I18nUtil.getBundleString(bundle, MainFrame.class, "application.Title"));
		this.getSearchButton().setText( I18nUtil.getBundleString(bundle, MiniDialog.class, "searchButton.Text"));
		
	}

	@Override
	public void mouseDragged(MouseEvent e) { 
		// 拖动（mouseDragged
		Object obj = e.getSource();
		if (obj==this.lb_name || obj==this.lb_ico) {
			// 指的不是鼠标在窗口中移动，而是用鼠标拖动）
			Point p = this.getLocation(); // 当鼠标拖动时获取窗口当前位置
			// 设置窗口的位置
			// 窗口当前的位置 + 鼠标当前在窗口的位置 - 鼠标按下的时候在窗口的位置
			this.setLocation(p.x + e.getX() - origin.x, p.y + e.getY() - origin.y);
		}
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) { 
		// 按下（mousePressed			
		Object obj = e.getSource();
		if (obj==this.lb_name || obj==this.lb_ico) {
			origin.x = e.getX(); // 当鼠标按下的时候获得窗口当前的位置
			origin.y = e.getY();
		}
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		Object obj = e.getSource();
		if (obj==this.lb_name || obj==this.lb_ico) {
			setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
		}
	}

	@Override
	public void mouseExited(MouseEvent e) {
		Object obj = e.getSource();
		if (obj==this.lb_name || obj==this.lb_ico) {
			setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
		}
	}

}
